import requests
from bs4 import BeautifulSoup

"""
流程分析
1. 通过requests发送请求，得到搜狗微信页面内容
2. 通过BeautifulSoup调用select获取指定的标签
3. 对提取到的标签 取出想要的数据
"""

# 1. 通过requests发送请求，得到搜狗微信页面内容
url = "https://weixin.sogou.com/"
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
}
r = requests.get(url=url, headers=headers)
r.encoding = "utf-8"
# print(r.text)

# 2. 通过BeautifulSoup调用select获取指定的标签
soup = BeautifulSoup(r.text, features="lxml")
ul_tag = soup.select('ul[class="news-list"]')
# print(ul_tag)
# print(len(ul_tag))  # 选择出来的只有1个标签，所以这个打印的结果为1

h3_list = ul_tag[0].select("h3")
for temp_tag in h3_list:
    print(temp_tag.select("a")[0].get_text(), temp_tag.select("a")[0].get("href"))
    print()
